package com.jfeat.base;

import com.jfeat.AmApplication;
import com.jfeat.am.config.properties.JwtProperties;
import com.jfeat.am.module.systemconfig.services.crud.service.SystemConfigService;
import com.jfeat.am.config.properties.ThirdPartyProperties;
import com.jfeat.am.module.wegou.services.patch.DealExchangeRateService;
import com.jfeat.am.module.wegou.services.patch.impl.DealExchangeRateServiceImpl;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.Resource;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;


/**
 * 基础测试类
 *
 * @author Admin
 * @Date 2017/5/21 16:10
 */
@ActiveProfiles(profiles = "dev")
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AmApplication.class)
@WebAppConfiguration
@Transactional //测试之后数据可回滚
public class BaseJunit {

    private static final Logger logger = LoggerFactory.getLogger(BaseJunit.class);


    @Autowired
    WebApplicationContext webApplicationContext;
    @Autowired
    ThirdPartyProperties thirdPartyProperties;
    @Autowired
    JwtProperties jwtProperties;
    @Autowired
    DealExchangeRateService dealExchangeRateService;

    DealExchangeRateServiceImpl dealExchangeRateServiceimpl = new DealExchangeRateServiceImpl();

    @Autowired
    SystemConfigService systemConfigService;

    protected MockMvc mockMvc;

    @Before
    public void setupMockMvc(){
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Before
    public void initDatabase(){
    }

    private String data = "{\"error_code\":0,\"resultcode\":\"200\",\"reason\":\"SUCCESSED!\",\"result\":[{\"data1\":{\"bankConversionPri\":\"634.8200\",\"date\":\"2018-02-23\",\"fBuyPri\":\"632.9100\",\"fSellPri\":\"635.5900\",\"mBuyPri\":\"627.7600\",\"mSellPri\":\"635.5900\",\"name\":\"美元\",\"time\":\"13:55:02\"},\"data2\":{\"bankConversionPri\":\"782.7600\",\"date\":\"2018-02-23\",\"fBuyPri\":\"777.7600\",\"fSellPri\":\"783.5000\",\"mBuyPri\":\"753.5900\",\"mSellPri\":\"785.0600\",\"name\":\"欧元\",\"time\":\"13:55:02\"},\"data3\":{\"bankConversionPri\":\"81.1400\",\"date\":\"2018-02-23\",\"fBuyPri\":\"80.9000\",\"fSellPri\":\"81.2200\",\"mBuyPri\":\"80.2600\",\"mSellPri\":\"81.2200\",\"name\":\"港币\",\"time\":\"13:55:02\"},\"data4\":{\"bankConversionPri\":\"5.9465\",\"date\":\"2018-02-23\",\"fBuyPri\":\"5.9095\",\"fSellPri\":\"5.9529\",\"mBuyPri\":\"5.7258\",\"mSellPri\":\"5.9529\",\"name\":\"日元\",\"time\":\"13:55:02\"},\"data5\":{\"bankConversionPri\":\"885.9500\",\"date\":\"2018-02-23\",\"fBuyPri\":\"881.5400\",\"fSellPri\":\"888.0300\",\"mBuyPri\":\"854.1500\",\"mSellPri\":\"889.9800\",\"name\":\"英镑\",\"time\":\"13:55:02\"},\"data6\":{\"bankConversionPri\":\"497.6200\",\"date\":\"2018-02-23\",\"fBuyPri\":\"494.5500\",\"fSellPri\":\"498.1900\",\"mBuyPri\":\"479.1900\",\"mSellPri\":\"499.2800\",\"name\":\"澳大利亚元\",\"time\":\"13:55:02\"},\"data7\":{\"bankConversionPri\":\"499.6100\",\"date\":\"2018-02-23\",\"fBuyPri\":\"497.0600\",\"fSellPri\":\"500.7300\",\"mBuyPri\":\"481.3700\",\"mSellPri\":\"501.8300\",\"name\":\"加拿大元\",\"time\":\"13:55:02\"},\"data8\":{\"bankConversionPri\":\"20.1700\",\"date\":\"2018-02-23\",\"fBuyPri\":\"20.0600\",\"fSellPri\":\"20.2200\",\"mBuyPri\":\"19.4400\",\"mSellPri\":\"20.8400\",\"name\":\"泰国铢\",\"time\":\"13:55:02\"},\"data9\":{\"bankConversionPri\":\"481.3700\",\"date\":\"2018-02-23\",\"fBuyPri\":\"477.6500\",\"fSellPri\":\"481.0100\",\"mBuyPri\":\"462.9100\",\"mSellPri\":\"482.2100\",\"name\":\"新加坡元\",\"time\":\"13:55:02\"},\"data10\":{\"bankConversionPri\":\"80.8700\",\"date\":\"2018-02-23\",\"fBuyPri\":\"80.3100\",\"fSellPri\":\"80.9500\",\"mBuyPri\":\"77.8300\",\"mSellPri\":\"81.1100\",\"name\":\"挪威克朗\",\"time\":\"13:55:02\"},\"data11\":{\"bankConversionPri\":\"465.2800\",\"date\":\"2018-02-23\",\"fBuyPri\":\"461.2700\",\"fSellPri\":\"464.5100\",\"mBuyPri\":\"447.0400\",\"mSellPri\":\"470.2000\",\"name\":\"新西兰元\",\"time\":\"13:55:02\"},\"data12\":{\"bankConversionPri\":\"105.1000\",\"date\":\"2018-02-23\",\"fBuyPri\":\"104.3600\",\"fSellPri\":\"105.2000\",\"mBuyPri\":\"101.1400\",\"mSellPri\":\"105.4100\",\"name\":\"丹麦克朗\",\"time\":\"13:55:02\"},\"data13\":{\"bankConversionPri\":\"78.2000\",\"date\":\"2018-02-23\",\"fBuyPri\":\"77.6600\",\"fSellPri\":\"78.2800\",\"mBuyPri\":\"75.2600\",\"mSellPri\":\"78.4400\",\"name\":\"瑞典克朗\",\"time\":\"13:55:02\"},\"data14\":{\"bankConversionPri\":\"12.2200\",\"date\":\"2018-02-23\",\"fBuyPri\":\"12.1700\",\"fSellPri\":\"12.2700\",\"mBuyPri\":\"11.8000\",\"mSellPri\":\"12.8400\",\"name\":\"菲律宾比索\",\"time\":\"13:55:02\"},\"data15\":{\"bankConversionPri\":\"11.2300\",\"date\":\"2018-02-23\",\"fBuyPri\":\"11.1800\",\"fSellPri\":\"11.2600\",\"mBuyPri\":\"10.4900\",\"mSellPri\":\"11.6900\",\"name\":\"卢布\",\"time\":\"13:55:02\"},\"data16\":{\"bankConversionPri\":\"680.6700\",\"date\":\"2018-02-23\",\"fBuyPri\":\"675.8100\",\"fSellPri\":\"680.5500\",\"mBuyPri\":\"654.9500\",\"mSellPri\":\"682.2500\",\"name\":\"瑞士法郎\",\"time\":\"13:55:02\"},\"data17\":{\"bankConversionPri\":\"21.7000\",\"date\":\"2018-02-23\",\"fBuyPri\":null,\"fSellPri\":null,\"mBuyPri\":\"20.9200\",\"mSellPri\":\"22.5500\",\"name\":\"新台币\",\"time\":\"13:55:02\"},\"data18\":{\"bankConversionPri\":\"78.8200\",\"date\":\"2018-02-23\",\"fBuyPri\":\"78.6300\",\"fSellPri\":\"78.9400\",\"mBuyPri\":\"75.9900\",\"mSellPri\":\"81.4700\",\"name\":\"澳门元\",\"time\":\"13:55:02\"},\"data19\":{\"bankConversionPri\":\"162.2700\",\"date\":\"2018-02-23\",\"fBuyPri\":\"163.2800\",\"fSellPri\":\"164.4200\",\"mBuyPri\":null,\"mSellPri\":null,\"name\":\"林吉特\",\"time\":\"13:55:02\"},\"data20\":{\"bankConversionPri\":\"54.4800\",\"date\":\"2018-02-23\",\"fBuyPri\":\"54.1100\",\"fSellPri\":\"54.4700\",\"mBuyPri\":\"49.9500\",\"mSellPri\":\"58.6300\",\"name\":\"南非兰特\",\"time\":\"13:55:02\"},\"data21\":{\"bankConversionPri\":\"0.5873\",\"date\":\"2018-02-23\",\"fBuyPri\":\"0.5851\",\"fSellPri\":\"0.5897\",\"mBuyPri\":\"0.5645\",\"mSellPri\":\"0.6112\",\"name\":\"韩国元\",\"time\":\"13:55:02\"}}]}";
    @Test
    public void test(){
        dealExchangeRateServiceimpl.dealExchangeRate(data);
    }

    @Test
    public void test1(){
        System.out.print("******************************************" + dealExchangeRateService.getSystemConfigByKey("日元").toString());
    }

    @Test
    public void test2() throws Exception{
        RequestBuilder request = get("/api/printerlist/42");
        MvcResult result = mockMvc.perform(request).andExpect(status().isOk()).andReturn();
        logger.debug(result.getResponse().getContentAsString());
    }

    @Test
    public void  testGetYml(){
        logger.info("===========key {}", thirdPartyProperties.getApiKey());
        logger.info(jwtProperties.getEncodedKey());
    }
}
